import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileReader;
import java.io.InputStream;
import java.util.Properties;
import java.util.StringTokenizer;

/*
 * 1. Correct the file name of config files
 * 2. only reading common.cfg also need to handle peer.cfg
 */

public class FileProperties {

	private int numberOfPreferredNeighbors;
	private int unchokingInterval;
	private int optimisticUnchokingInterval;
	private long fileSize;
	private int pieceSize;
	private int totalNumPieces;
	private String fileName;
	private int numOfPeers;
	int peerIDArray[];
	String peerHostNames[];
	int peerPortNumbers[];
	private boolean hasCompleteFile;
	
	/*
	 * Init without real config files
	 */
	public FileProperties(boolean autoInit){
		pieceSize = 1;
		totalNumPieces = 5;
		numOfPeers = 2;
		peerIDArray = new int[5];
		peerIDArray[0] = 1001;
		peerIDArray[1] = 1002;
		peerHostNames = new String[2];
		peerHostNames[0] = "127.0.0.1";
		peerHostNames[1] = "127.0.0.1";
		
		if (autoInit){
			setHasCompleteFile(true);
			// init rest of the stuff
		}else{
			setHasCompleteFile(false);
			// init rest of the stuff
		}
	}

	public FileProperties() {
		try {
			Properties prop = new Properties();
			InputStream is = new FileInputStream(
					"C://Users//Sirisha//workspace//BitTorrentCN//src//common.cfg");
			prop.load(is);
	
			// Read the properties file parameters...
			setNumberOfPreferredNeighbors(Integer.parseInt(prop
					.getProperty("NumberOfPreferredNeighbors")));
			setUnchokingInterval(Integer.parseInt(prop
					.getProperty("UnchokingInterval")));
			setOptimisticUnchokingInterval(Integer.parseInt(prop
					.getProperty("OptimisticUnchokingInterval")));
			fileName = prop.getProperty("FileName");
			setFileSize(Integer.parseInt(prop.getProperty("FileSize")));
			
			// check if can be parsed to long
			setPieceSize(Integer.parseInt(prop.getProperty("PieceSize")));
			
			//set number of pieces
			setTotalNumPieces(getFileSize(), getPieceSize());

			// Read peerdata.cfg
			FileReader fr = new FileReader(
					"C://Users//Sirisha//workspace//BitTorrentCN//src//PeerInfo.cfg");
			BufferedReader br = new BufferedReader(fr);
			String read;
			StringTokenizer readTokenizer;
			int peersCount = 0;

			// read number of peers (lines) in file
			br.mark(9999999);
			while ((read = br.readLine()) != null) {
				peersCount++;
			}
			setPeerCount(peersCount);
			// go to the start of file
			br.reset();
			peerIDArray = new int[getNumPeers()];
			peerHostNames = new String[getNumPeers()];
			// read the parameters into peerInfo
			int index = 0;
			while ((read = br.readLine()) != null) {
				readTokenizer = new StringTokenizer(read);
				peerIDArray[index] = Integer
						.parseInt(readTokenizer.nextToken());
				peerHostNames[index] = readTokenizer.nextToken();
				peerPortNumbers[index] = Integer.parseInt(readTokenizer
						.nextToken());
				if (Integer.parseInt(readTokenizer.nextToken()) == 1) {
					setHasCompleteFile(true);
				} else {
					setHasCompleteFile(false);
				}
				index++;
			}
			System.out.println("NO Error reading cfg");

		} catch (Exception e) {
			System.out.println("Error reading config files!");
			e.printStackTrace();
		}
	}

	private void setPeerCount(int peerCount) {
		this.numOfPeers = peerCount;
	}

	public int getNumPeers() {
		return numOfPeers;
	}

	private void setNumberOfPreferredNeighbors(int numberOfPreferredNeighbors) {
		this.numberOfPreferredNeighbors = numberOfPreferredNeighbors;
	}

	private int getNumberOfPreferredNeighbors() {
		return numberOfPreferredNeighbors;
	}

	private void setOptimisticUnchokingInterval(int optimisticUnchokingInterval) {
		this.optimisticUnchokingInterval = optimisticUnchokingInterval;
	}

	public int getOptimisticUnchokingInterval() {
		return optimisticUnchokingInterval;
	}

	private void setFileSize(long fileSize) {
		this.fileSize = fileSize;
	}

	public long getFileSize() {
		return fileSize;
	}

	private void setPieceSize(int pieceSize) {
		this.pieceSize = pieceSize;
	}

	public int getPieceSize() {
		return pieceSize;
	}

	private void setUnchokingInterval(int unchokingInterval) {
		this.unchokingInterval = unchokingInterval;
	}

	public int getUnchokingInterval() {
		return unchokingInterval;
	}

	private void setTotalNumPieces(long fileSize, int pieceSize) {
		this.totalNumPieces = (int) Math.ceil(fileSize/pieceSize);
	}

	public int getTotalNumPieces() {
		return totalNumPieces;
	}

	private void setHasCompleteFile(boolean hasCompleteFile) {
		this.hasCompleteFile = hasCompleteFile;
	}

	public boolean hasCompleteFile() {
		return hasCompleteFile;
	}
}
